package com.hch.web.admin;


import com.hch.domain.Blog;
import com.hch.domain.User;
import com.hch.service.BlogService;
import com.hch.service.TagService;
import com.hch.service.TypeService;
import com.hch.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String list(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                               Pageable pageable, BlogQuery blogQuery, Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                 Pageable pageable, BlogQuery blogQuery, Model model) {
        model.addAttribute("page", blogService.listBlog(pageable, blogQuery));
        return "admin/blogs :: blogList";
    }

    //跳转到博客新增页面
    @GetMapping("/blogs/input")
    public String input(Model model) {
        setTypeAndTag(model);
        //初始化，让页面拿到一些属性值
        model.addAttribute("blog", new Blog());
        return INPUT;
    }

    /**
     * 初始化分类与标签，避免耦合
     * @param model
     */
    private void setTypeAndTag(Model model){
        //初始化分类
        model.addAttribute("types", typeService.listType());
        //初始化标签
        model.addAttribute("tags", tagService.listTag());
    }

    //跳转到博客修改页面
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        blog.init();    //把tagIds处理成字符串
        //model.addAttribute("blog", blogService.getBlog(id));
        //将根据id查找到的属性值保存在model中方便前端调用
        model.addAttribute("blog", blog);
        return INPUT;
    }
    //新增博客和更新博客共用这个方法
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes redirectAttributes, HttpSession session) {
        blog.setUser((User)session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));//根据前端选择的分类id查询到分类名后获取id再设置Type，给👴整吐了
        //设置所选的标签ids
        blog.setTags(tagService.listTags(blog.getTagIds()));
        Blog b;
        //判断是否
        if (blog.getId() == null) {
            //id为空说明是新增的博客
            b = blogService.saveBlog(blog);
        } else {
            //不为空说明是修改的博客
            b = blogService.updateBlog(blog.getId(),blog);
        }
        //做个简单校验,仅在前端做校验
        if (b == null) {
            //如果更新失败，给个提示
            redirectAttributes.addFlashAttribute("message", "操作失败");
        } else {
            //更新成功，给个提示
            redirectAttributes.addFlashAttribute("message", "操作成功");
        }
        return REDIRECT_LIST;//返回博客列表页面
    }

    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes) {
        blogService.deleteBlog(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return REDIRECT_LIST;
    }

}
